/// <reference path="../../../inig.d.ts" />

import axios, { Axios, AxiosAdapter, AxiosRequestConfig } from 'axios'
// import mpAdapter from 'axios-miniprogram-adapter'
import { GlobalData } from '../global'

export class InigRequest {
  private static instance: InigRequest

  private static axiosInstance: Axios

  constructor() { }

  static initAxiosInstance() {
    let timeout = GlobalData.timeout
    let createParams = <AxiosRequestConfig>{
      withCredentials: true,
    }
    createParams.timeout = Number(timeout)
    if (createParams.timeout == 0) {
      // web平台timeout为0时，表示设置0ms超时，适配成0表示不超时
      createParams.timeout = 9999999
    }

    // if (GlobalData.platform == 'mpapp') {
    //   // 小程序Adapter
    //   axios.defaults.adapter = mpAdapter as AxiosAdapter
    // }
    this.axiosInstance = axios.create(createParams)
  }

  public static getInstance() {
    if (!this.instance) {
      this.instance = new InigRequest()
      this.initAxiosInstance()
    }
    return this.instance
  }

  public get(url: string, params?: object): Promise<any> {
    let baseData = GlobalData.base_data
    return InigRequest.axiosInstance.get(url, {
      params: Object.assign({}, { ...baseData }, params),
    })
  }
  public post(url: string, params?: object): Promise<any> {
    let baseData = GlobalData.base_data
    return InigRequest.axiosInstance.post(url, { ...baseData, ...params })
  }
}
